package cn.iocoder.yudao.module.member.controller.admin.studentShow;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.studentShow.vo.*;
import cn.iocoder.yudao.module.member.convert.studentShow.StudentShowConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.studentShow.StudentShowDO;
import cn.iocoder.yudao.module.member.service.studentShow.StudentShowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -运营端- 学生风采")
@RestController
@RequestMapping("/member/student-show")
@Validated
public class StudentShowController {

    @Resource
    private StudentShowService studentShowService;

    @PostMapping("/create")
    @ApiOperation("创建学生风采")
    @TenantIgnore
    public CommonResult<Long> createStudentShow(@Valid @RequestBody StudentShowCreateReqVO createReqVO) {
        return success(studentShowService.createStudentShow(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新学生风采")
    @TenantIgnore
    public CommonResult<Boolean> updateStudentShow(@Valid @RequestBody StudentShowUpdateReqVO updateReqVO) {
        studentShowService.updateStudentShow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除学生风采")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<Boolean> deleteStudentShow(@RequestParam("id") Long id) {
        studentShowService.deleteStudentShow(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得学生风采")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @TenantIgnore
    public CommonResult<StudentShowRespVO> getStudentShow(@RequestParam("id") Long id) {
        StudentShowDO studentShow = studentShowService.getStudentShow(id);
        return success(StudentShowConvert.INSTANCE.convert(studentShow));
    }

    @GetMapping("/list")
    @ApiOperation("获得学生风采列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @TenantIgnore
    public CommonResult<List<StudentShowRespVO>> getStudentShowList(@RequestParam("ids") Collection<Long> ids) {
        List<StudentShowDO> list = studentShowService.getStudentShowList(ids);
        return success(StudentShowConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得学生风采分页")
    @TenantIgnore
    public CommonResult<PageResult<StudentShowRespVO>> getStudentShowPage(@Valid StudentShowPageReqVO pageVO) {
        PageResult<StudentShowDO> pageResult = studentShowService.getStudentShowPage(pageVO);
        return success(StudentShowConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出学生风采 Excel")
    @TenantIgnore
    @OperateLog(type = EXPORT)
    public void exportStudentShowExcel(@Valid StudentShowExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudentShowDO> list = studentShowService.getStudentShowList(exportReqVO);
        // 导出 Excel
        List<StudentShowExcelVO> datas = StudentShowConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "学生风采.xls", "数据", StudentShowExcelVO.class, datas);
    }


    @GetMapping("/recoveryStudentShowPage")
    @ApiOperation("回收站(学生风采)")
    @TenantIgnore
    public CommonResult<PageResult<StudentShowRespVO>> recoveryStudentShowPage(@Valid StudentShowPageReqVO pageVO) {
        PageResult<StudentShowDO> pageResult = studentShowService.recoveryStudentShowPage(pageVO);
        return success(StudentShowConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getRecoveryStudentShowById")
    @ApiOperation("回收站(根据ID查看)")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<StudentShowRespVO> getRecoveryStudentShowById(@RequestParam("id") Long id) {
        StudentShowDO StudentShowDO = studentShowService.getRecoveryStudentShowById(id);
        StudentShowRespVO StudentShowRespVO=null;
        if(null!=StudentShowDO){
            StudentShowRespVO=new StudentShowRespVO();
            BeanUtils.copyProperties(StudentShowDO,StudentShowRespVO);
        }
        return success(StudentShowRespVO);
    }
    @PutMapping("/updateBatchRecoveryStudentShow")
    @ApiOperation("回收站(批量还原)")
    @TenantIgnore
    public CommonResult<Boolean> updateBatchRecoveryStudentShow( @RequestBody StudentShowIdsVO studentShowIdsVO) {
        if(CollectionUtils.isEmpty(studentShowIdsVO.getIds())){
            return error("请先选择记录");
        }
        studentShowService.updateBatchRecoveryStudentShow(studentShowIdsVO.getIds());
        return success(true);
    }

}
